<?php
/**
 * Created by PhpStorm.
 * User: hetu
 * Date: 2018/5/7
 * Time: 15:57
 */

use \yii\helpers\Url;
use \yii\helpers\Html;

$orig_name = Yii::$app->request->get("role_name") ;
?>
<style type="text/css">
    .edit_role{
        list-style:none;
        width: 799px;
        float: left;
        margin-left: -25px;
    }
    .edit_role li{
        font-size: 12px;
        margin-right: 20px;
        line-height: 35px;
        width: 750px;
        overflow: auto;
    }
    .edit_role li span{
        margin-left: 15px;
    }
</style>
<form id="upd-role" class="form-horizontal" method="post">

    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="myModalLabel">编辑角色</h4>
    </div>
    <div style="border-bottom: 1px solid #e5e5e5"></div>
    <div class="modal-body">
        <div class="box-body">
            <div class="form-group">
                <label  class="col-sm-4 control-label" style="padding-top:4px">角色名称：</label>
                <input type="text" id="RoleForm-name"  name="RoleForm[name]" value="<?= $model->name ?>" maxlength="8">
            </div>
            <div class="form-group">
                <label class="col-sm-4 control-label" style="padding-top:4px">权限配置：</label>
                <div style="height: 400px;width: 350px;border: 1px solid #ababab ;overflow-x:hidden;overflow-y:scroll;">
                <ul class="edit_role">
                    <?php foreach ($column_all as $k => $val): ?>
                        <li>
                            <div style="width: 80px; float:left;"><label><input name="selectall" type="checkbox" style="vertical-align: text-bottom;">&nbsp;&nbsp;<?= $val['tag_name']?>:</label></div>
                            <div style="width: 600px; float:left;">
                                <?php foreach ($edit_all as $key => $value): ?>
                                    <?php if($val['tag']==$value['column_name']):?>
                                        <span><input name="RolePermissionForm[permission][]" type="checkbox" <?php if(in_array($value['name'],$edit_res)):?>checked="checked"<?php endif ;?> style="vertical-align: text-bottom;" value="<?= $value['name'] ?>"/>&nbsp;&nbsp;<?= $value['description'] ?></span><br/>
                                    <?php endif ;?>
                                <?php endforeach; ?>
                            </div>
                        </li>
                    <?php endforeach; ?>
                    <li>
                        <div style="width: 80px; float:left;"><label><input name="selectall" type="checkbox" style="vertical-align: text-bottom;">&nbsp;&nbsp;其他模块:</label></div>
                        <div style="width: 600px; float:left;">
                            <?php foreach ($edit_all as $key => $value): ?>
                                <?php if(!in_array($value['column_name'],$column_alls)):?>
                                    <span><input name="RolePermissionForm[permission][]" type="checkbox" <?php if(in_array($value['name'],$edit_res)):?>checked="checked"<?php endif ;?> style="vertical-align: text-bottom;" value="<?= $value['name'] ?>"/>&nbsp;&nbsp;<?= $value['description'] ?></span><br/>
                                <?php endif ;?>
                            <?php endforeach; ?>
                        </div>
                    </li>
                </ul>
                </div>
            </div>
        </div>
        <input type="hidden" id="RolePermissionForm[name][]" class="form-control" name="RolePermissionForm[name][]">
        <input type="hidden" id="RoleForm[orig_name][]" class="form-control" name="RoleForm[orig_name][]" value="<?=$orig_name?>">
    </div>
    <div style="border-bottom: 1px solid #e5e5e5"></div>
    <div class="modal-footer">
        <?= Html::hiddenInput('_csrf-backend',\Yii::$app->getRequest()->csrfToken) ?>
        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
        <button type="button" class="btn btn-primary" id="upd-role-ajax-submit">修改</button>
    </div>
</form>
<input type="hidden" id="get-role-name" class="form-control" name="get-role-name" data-id="<?= $role_name ?>">
<script>
    $(function(){
        $("#RolePermissionForm-name").val($("#RoleForm-name").val());
        $("#RoleForm-name").keyup(function(){
            var value = $(this).val();
            addvalue(value);
        });

        function addvalue(value)
        {
            var val = value;
            $("#RolePermissionForm-name").val(val);
        }
    });
    $('#upd-role-ajax-submit').click(function () {
        var data = $('#upd-role').serialize();
        var role_name = $('#get-role-name').data('id');
        var rolename = $('#role_name').val();
        sendAjax('<?= Url::toRoute('upd-role') ?>'+ '?role_name=' + role_name + "&rolename=" + rolename, data, $(this));
    });
    /**
    点击全选
     */
    $('input[name="selectall"]').click(function(){
        //alert(this.checked);
        if($(this).is(':checked')){
            $(this).parent().parent().next().find("input").each(function(){
                //此处如果用attr，会出现第三次失效的情况
                $(this).prop("checked",true);
            });
        }else{
            $(this).parent().parent().next().find("input").each(function(){
                $(this).removeAttr("checked",false);
            });
            //$(this).removeAttr("checked");
        }

    });
</script>